<?php
/**
 * Created by PhpStorm.
 * User: hetu
 * Date: 2018/5/9
 * Time: 16:49
 */

use yii\helpers\Url ;
use yii\helpers\Html ;
use backend\assets\AppAsset ;
use common\service\ConstatService;
use common\models\AdminUser;
use yii\widgets\LinkPager;
use backend\service\StoreService ;

AppAsset::addPageScript($this, "/js/admin.js") ;

$i = 1 ;
$this->params['breadcrumbs'][] = "模块管理";
$this->params['display_name'] = "用户列表" ;
$this->title = '用户列表-' . Yii::$app->params['webname'];
$status = [''=>'全部', ConstatService::ADMIN_USER_STATUS_ENABLE=>'可用',ConstatService::ADMIN_USER_STATUS_DISABLE=>'禁用'];
?>
<div class="row">
    <div class="col-md-12">
        <div class="nav-tabs-custom">
            <div class="tab-content">
                <div class="tab-pane active">
                    <form method="get" class="form-inline form">
                        <div class="box-body">
                            <div class="form-group">
                                <label class="control-label">姓名：</label>
                                <?= Html::input("text", "username",$model->username,["class"=>"form-control", 'maxlength' => 10]) ?>
                            </div>
                            <div class="form-group">
                                <label class="control-label">手机号码：</label>
                                <?= Html::input("text", "phone",$model->phone,["class"=>"form-control", 'maxlength' => 11, 'onkeyup' => 'value=value.replace(/[^\d]/g,\'\')']) ?>
                            </div>
                            <div class="form-group">
                                <label class="control-label">角色：</label>
                                <?= Html::dropDownList('role', $model->role, $role, ['class' => 'form-control']) ?>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-primary" type="submit">查询</button>
                                <button id="add_new" class="btn btn-primary btn-hide button-add-user" title="添加用户" type="button">添加用户</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div style="clear:both;"></div>
            </div>
            <div class="tab-content">
                <div class="tab-pane active">
                    <div class="box-body table-responsive no-padding">
                        <span>共<?= $pages->totalCount ?>条记录</span>
                        <table class="table table-bordered table-striped table-condensed">
                            <tbody>
                                <tr>
                                    <th>序号</th>
                                    <th>姓名</th>
                                    <th>手机号码</th>
                                    <th>状态</th>
                                    <th>角色</th>
                                    <th>创建人</th>
                                    <th>更新时间</th>
                                    <th>操作</th>
                                </tr>
                                <?php foreach ($list as $value):?>
                                <tr>
                                    <td><?= $i?></td>
                                    <td class="center"><?= $value['username'] ?></td>
                                    <td class="center"><?= $value['phone'] ?></td>
                                    <td class="center"><?= $status[$value['status']] ?></td>
                                    <td class="center"><?php if(isset($role[$value['role']]) && !empty($value['role'])){echo $role[$value['role']];}else{if($value['role']=="请选择"){echo "-";}else{echo $value['role'];};}?></td>
                                    <td class="center"><?= $value['create_user_id']?></td>
                                    <td class="center"><?= $value['uptime']?date("Y-m-d H:i:s",$value['uptime']):'-' ?></td>
                                    <td class="center">
                                        <button id="edit-user" class="btn btn-primary btn-xs edit btn-hide button-edit-user" title="修改用户" type="button" data-id="<?= $value['user_id']?>">编辑</button>
                                        <button class="btn btn-primary btn-xs del-user-btn btn-hide button-del-user" data-id="<?= $value['user_id']?>" type="button">删除</button>
                                    </td>
                                </tr>
                                <?php $i=$i+1?>
                                <?php endforeach;?>
                                <tr>
                                    <td colspan="17" class="text-center">
                                        <?=
                                        LinkPager::widget([
                                            'firstPageLabel' => '首页',
                                            'lastPageLabel' => '末页',
                                            'prevPageLabel' => '上一页',
                                            'nextPageLabel' => '下一页',
                                            'pagination' =>$pages,
                                            'maxButtonCount' => 8,
                                        ])
                                        ?>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div><!--/span-->
<script>
    var id;
    var search_query = window.location.search.replace('?', '');
    $(function(){
        $("#myModal").on("hidden.bs.modal", function() {
            $(this).removeData("bs.modal");
            $(this).find(".modal-content").children().remove();
        })
    });
    $('#add_new').click(function () {
        var options = {remote: '<?= Url::toRoute('add-user') ?>'};
        $('#myModal').modal(options)
    });
    $('.edit').click(function () {
        var user_id = $(this).data('id');
        var options = {remote: '<?= Url::toRoute('edit-user') ?>' + '?user_id=' + user_id + "&" + search_query};
        $('#myModal').modal(options)
    });
    $('.google-secret').click(function () {
        var user_id = $(this).data('id');
        var options = {remote: '<?= Url::toRoute('/google-authenticator/google-secret') ?>' + '?user_id=' + user_id + "&" + search_query};
        $('#myModal').modal(options)
    });
    $('.del-user-btn').click(function () {
        id = $(this).data('id');
        var tmpHtml = '<div class="modal-header">\n' +
            '                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
            '                <h4 class="modal-title">后台管理</h4>\n' +
            '            </div>\n' +
            '            <div class="modal-body">\n' +
            '                <h4>您确定要删除此用户吗?</h4>\n' +
            '            </div>\n' +
            '            <div class="modal-footer">\n' +
            '                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>\n' +
            '                <button type="button" class="btn btn-default modal-btn-sure button-del-user-confirm">确认</button>\n' +
            '            </div>';
        $("#myModal").find(".modal-content").html(tmpHtml);
        $('#myModal').modal('show');
    });
    $(document).on('click', '.button-del-user-confirm', function () {
        var user_id =  id;
        sendAjax('<?= Url::toRoute('del-user') ?>' + "?" + search_query, {user_id:user_id}, $(this));
    })

    $('.reset-password').click(function () {
        id = $(this).data('id');
        var tmpHtml = '<div class="modal-header">\n' +
            '                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
            '                <h4 class="modal-title">后台管理</h4>\n' +
            '            </div>\n' +
            '            <div class="modal-body">\n' +
            '                <h4>您确定要重置密码吗?</h4>\n' +
            '            </div>\n' +
            '            <div class="modal-footer">\n' +
            '                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>\n' +
            '                <button type="button" class="btn btn-default modal-btn-sure button-reset-password-confirm">确认</button>\n' +
            '            </div>';
        $("#myModal").find(".modal-content").html(tmpHtml);
        $('#myModal').modal('show');
    });
    $(document).on('click', '.button-reset-password-confirm', function () {
        var user_id =  id;
        sendAjax('<?= Url::toRoute('reset-passwords') ?>' + "?" + search_query + "&user_id=" + user_id, '' ,$(this));
    })


</script>